package cn.jinjimi.app.custom.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.jinjimi.base.util.AndroidUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;

/**
 * 所有客户列表的adapter
 * <p/>
 * Created by jinsen on 15/10/22.
 */
public class CustomerAllAdapter extends MyBaseAdapter<Custom> {

    private Context mContext;

    private boolean ifShowSamePhone;

    public CustomerAllAdapter(Context context) {
        this.mContext = context;
    }

    public void setIfShowSamePhone(boolean ifShowSamePhone) {
        this.ifShowSamePhone = ifShowSamePhone;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        final Custom custom = getItem(position);

        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_all_customer_layout, null);
            holder = new Holder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        holder.tvName.setText(custom.name);

        holder.llName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onActionClickListener != null) {
                    onActionClickListener.onItemClick(custom);
                }
            }
        });

        holder.llName.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onActionClickListener != null) {
                    onActionClickListener.onItemLongClick(custom);
                }
                return false;
            }
        });

        if (ifShowSamePhone) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.tvName.getLayoutParams();
            layoutParams.setMargins(0, 0, mContext.getResources().getDimensionPixelOffset(R.dimen.dp_7), 0);
            holder.tvName.setLayoutParams(layoutParams);
            holder.tvName.setMaxWidth(AndroidUtils.getWindowWidth(mContext) * 2 / 3);

            holder.tvSamePhone.setVisibility(View.VISIBLE);
        } else {
            holder.tvSamePhone.setVisibility(View.GONE);
        }

        return convertView;
    }

    class Holder {
        TextView tvName;
        TextView tvIndex;
        LinearLayout llIndex;
        View viewLine;
        LinearLayout llName;

        TextView tvSamePhone;

        public Holder(View view) {
            tvName = (TextView) view.findViewById(R.id.tv_name);
            llName = (LinearLayout) view.findViewById(R.id.ll_name);
            tvIndex = (TextView) view.findViewById(R.id.item_all_customer_index);
            llIndex = (LinearLayout) view.findViewById(R.id.ll_index);
            viewLine = view.findViewById(R.id.view_line);

            tvSamePhone = (TextView) view.findViewById(R.id.tv_same_phone);
        }
    }

    public interface onActionClickListener {
        void onItemClick(Custom custom);

        void onItemLongClick(Custom custom);
    }

    public onActionClickListener onActionClickListener;

    public void setOnActionClickListener(onActionClickListener onActionClickListener) {
        this.onActionClickListener = onActionClickListener;
    }

}